import os
import xlrd
import re
import  datetime 
import xlwt


choice_questions_array = []
judgement_questions_array = []
debate_choice_questions_array = []
debate_judgement_questions_array = []
choice_repeated_count = 0
choice_total_count = 0
judgement_repeated_count = 0
judgement_total_count = 0
items = os.listdir(os.getcwd())

for item in items:
  if 'chapter' in item:
    if os.path.isdir(item):
        for file in os.listdir(item):
            if '.DS_Store' in file:
              continue
            xlsx = xlrd.open_workbook('%s/%s' % (item,file))
            choice_sheet = xlsx.sheets()[0]
            judgement_sheet = xlsx.sheets()[1]
            for i in range(1,choice_sheet.nrows):  
               if (choice_sheet.row_values(i)[1]):
               	    choice_question = {
                        "content":choice_sheet.row_values(i)[1],
                        "choice_A":choice_sheet.row_values(i)[2],
                        "choice_B":choice_sheet.row_values(i)[3],
                        "choice_C":choice_sheet.row_values(i)[4],
                        "choice_D":choice_sheet.row_values(i)[5],
                        "right_choice":choice_sheet.row_values(i)[6],
                        "file_name":file,
                        "repeated_time":1
                    }
                    find_repeated = 0
                    for temp_choice_question in choice_questions_array:
                       if (temp_choice_question['content'] == choice_question['content']):
                       	    temp_choice = temp_choice_question['right_choice']
                       	    temp_choice_content = ''
                       	    if temp_choice == 'A' or  temp_choice == 'a' or  temp_choice == '1':
                       	    	temp_choice_content = temp_choice_question['choice_A'].strip()
                       	    elif temp_choice == 'B' or  temp_choice == 'b' or  temp_choice == '2':
                       	    	temp_choice_content = temp_choice_question['choice_B'].strip()
                       	    elif temp_choice == 'C' or  temp_choice == 'c' or  temp_choice == '3':
                       	    	temp_choice_content = temp_choice_question['choice_C'].strip()
                       	    else :
                       	    	temp_choice_content = temp_choice_question['choice_D'].strip()
                       	    the_choice = choice_question['right_choice']
                       	    the_choice_content = ''
                       	    if the_choice == 'A' or  the_choice == 'a' or  the_choice == '1':
                       	    	the_choice_content = choice_question['choice_A'].strip()
                       	    elif the_choice == 'B' or  the_choice == 'b' or  the_choice == '2':
                       	    	the_choice_content = choice_question['choice_B'].strip()
                       	    elif the_choice == 'C' or  the_choice == 'c' or  the_choice == '3':
                       	    	the_choice_content = choice_question['choice_C'].strip()
                       	    else :
                       	    	the_choice_content = choice_question['choice_D'].strip()
                       	    if temp_choice_content != the_choice_content:
                       	        debate_choice_questions_array.append(choice_question)
                            find_repeated = 1
                            temp_choice_question['repeated_time'] = temp_choice_question['repeated_time']+1
                    if find_repeated == 0:
                      choice_questions_array.append(choice_question)
                    else:
                      choice_repeated_count = choice_repeated_count+1
                    choice_total_count = choice_total_count+1
            for i in range(1,judgement_sheet.nrows):  
               if (judgement_sheet.row_values(i)[1]):
                    content = judgement_sheet.row_values(i)[1]
                    find_repeated = 0
                    judgement_question = {
                       "content":judgement_sheet.row_values(i)[1],
                       "right_choice":judgement_sheet.row_values(i)[2],
                       "file_name":file,
                       "repeated_time":1
                    }
                    for temp_judgement_question in judgement_questions_array:
                       if (temp_judgement_question['content'] == content):
                       	    temp_choice = temp_judgement_question['right_choice'] 
                       	    the_choice = judgement_question['right_choice']
                       	    if temp_choice != the_choice:
                       	       debate_judgement_questions_array.append(judgement_question)
                            find_repeated = 1
                            temp_judgement_question['repeated_time'] = temp_judgement_question['repeated_time']+1
                    if find_repeated == 0:
                       judgement_questions_array.append(judgement_question)
                    else:
                       judgement_repeated_count = judgement_repeated_count+1
                    judgement_total_count = judgement_total_count+1


workbook = xlwt.Workbook(encoding='utf-8')
choice_sheet = workbook.add_sheet('选择题')
choice_sheet.col(0).width = 256 * 80  # Set the column width
choice_sheet.col(1).width = 256 * 40  # Set the column width
choice_sheet.col(2).width = 256 * 40  # Set the column width
choice_sheet.col(3).width = 256 * 40  # Set the column width
choice_sheet.col(4).width = 256 * 40  # Set the column width

choice_sheet.write(0, 0, '题目')
choice_sheet.write(0, 1, '选项A')
choice_sheet.write(0, 2, '选项B')
choice_sheet.write(0, 3, '选项C')
choice_sheet.write(0, 4, '选项D')
choice_sheet.write(0, 5, '正确选项')
choice_sheet.write(0, 6, '出现次数')
for i in range(0,len(choice_questions_array)):
  choice_question = choice_questions_array[i]
  choice_sheet.row(i+1).height_mismatch = True
  choice_sheet.row(i+1).height = 20 * 40  # Set the column width
  choice_sheet.write(i+1, 0, choice_question['content'])
  choice_sheet.write(i+1, 1, choice_question['choice_A'])
  choice_sheet.write(i+1, 2, choice_question['choice_B'])
  choice_sheet.write(i+1, 3, choice_question['choice_C'])
  choice_sheet.write(i+1, 4, choice_question['choice_D'])
  choice_sheet.write(i+1, 5, choice_question['right_choice'])
  choice_sheet.write(i+1, 6, choice_question['repeated_time'])

judgement_sheet = workbook.add_sheet('判断题')
judgement_sheet.col(0).width = 256 * 80  # Set the column width
judgement_sheet.col(1).width = 256 * 60  # Set the column width
judgement_sheet.write(0, 0, '题目')
judgement_sheet.write(0, 1, '正确选项')
judgement_sheet.write(0, 2, '出现次数')
for i in range(0,len(judgement_questions_array)):
  judgement_question = judgement_questions_array[i]
  judgement_sheet.row(i+1).height_mismatch = True
  judgement_sheet.row(i+1).height = 20 * 40  # Set the column width
  judgement_sheet.write(i+1, 0, judgement_question['content'])
  judgement_sheet.write(i+1, 1, judgement_question['right_choice'])
  judgement_sheet.write(i+1, 2, judgement_question['repeated_time'])

debate_choice_sheet = workbook.add_sheet('争议选择题')
debate_choice_sheet.col(0).width = 256 * 80  # Set the column width
debate_choice_sheet.col(1).width = 256 * 40  # Set the column width
debate_choice_sheet.col(2).width = 256 * 40  # Set the column width
debate_choice_sheet.col(3).width = 256 * 40  # Set the column width
debate_choice_sheet.col(4).width = 256 * 40  # Set the column width

debate_choice_sheet.write(0, 0, '题目')
debate_choice_sheet.write(0, 1, '选项A')
debate_choice_sheet.write(0, 2, '选项B')
debate_choice_sheet.write(0, 3, '选项C')
debate_choice_sheet.write(0, 4, '选项D')
debate_choice_sheet.write(0, 5, '正确选项')
for i in range(0,len(debate_choice_questions_array)):
  choice_question = debate_choice_questions_array[i]
  debate_choice_sheet.row(i+1).height_mismatch = True
  debate_choice_sheet.row(i+1).height = 20 * 40  # Set the column width
  debate_choice_sheet.write(i+1, 0, choice_question['content'])
  debate_choice_sheet.write(i+1, 1, choice_question['choice_A'])
  debate_choice_sheet.write(i+1, 2, choice_question['choice_B'])
  debate_choice_sheet.write(i+1, 3, choice_question['choice_C'])
  debate_choice_sheet.write(i+1, 4, choice_question['choice_D'])
  debate_choice_sheet.write(i+1, 5, choice_question['right_choice'])

debate_judgement_sheet = workbook.add_sheet('争议判断题')
debate_judgement_sheet.col(0).width = 256 * 80  # Set the column width
debate_judgement_sheet.col(1).width = 256 * 60  # Set the column width
debate_judgement_sheet.write(0, 0, '题目')
debate_judgement_sheet.write(0, 1, '正确选项')
for i in range(0,len(debate_judgement_questions_array)):
  judgement_question = debate_judgement_questions_array[i]
  debate_judgement_sheet.row(i+1).height_mismatch = True
  debate_judgement_sheet.row(i+1).height = 20 * 40  # Set the column width
  debate_judgement_sheet.write(i+1, 0, judgement_question['content'])
  debate_judgement_sheet.write(i+1, 1, judgement_question['right_choice'])

date=datetime.datetime.now().strftime('%Y%m%d%h%m%s')
path=date+'-result'
workbook.save('%s.xls' % (path))






